package com.huawei.esdk.tp.healthcare.local.impl.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.xml.ws.Holder;

import com.huawei.esdk.tp.healthcare.local.bean.CameraControl;
import com.huawei.esdk.tp.healthcare.local.bean.SiteInfo;
import com.huawei.esdk.tp.healthcare.local.bean.TPSDKResponse;
import com.huawei.esdk.tp.healthcare.local.constant.NativeConstant;
import com.huawei.esdk.tp.healthcare.local.impl.autogen.SitesStatusMap;
import com.huawei.esdk.tp.healthcare.local.impl.autogen.TPHealthcareSiteCtr;
import com.huawei.esdk.tp.healthcare.local.impl.autogen.TPHealthcareSiteMgr;
import com.huawei.esdk.tp.healthcare.local.impl.utils.ClientProvider;
import com.huawei.esdk.tp.healthcare.local.impl.utils.ExceptionUtils;
import com.huawei.esdk.tp.healthcare.local.site.SiteService;

public class SiteServiceImpl implements SiteService {
	private TPHealthcareSiteMgr tPHealthcareSiteMgr = ((TPHealthcareSiteMgr) ClientProvider
			.getClient(TPHealthcareSiteMgr.class));
	private TPHealthcareSiteCtr tPHealthcareSiteCtr = ((TPHealthcareSiteCtr) ClientProvider
			.getClient(TPHealthcareSiteCtr.class));
	private static SiteServiceImpl instance = null;
	
	public static synchronized SiteServiceImpl getInstance()
	{
		if (null == instance) {
			instance = new SiteServiceImpl();
		}
		return instance;
	}

	
	public TPSDKResponse<List<SiteInfo>> queryAllSites() {
		Holder<Integer> sdkResponse = new Holder<Integer>();
		TPSDKResponse<List<SiteInfo>> resp = new TPSDKResponse<List<SiteInfo>>();
		Holder<List<SiteInfo>> sitesInfo = new Holder<List<SiteInfo>>();
		try {
			tPHealthcareSiteMgr.queryAllSites(sdkResponse, sitesInfo);
		} catch (Exception e) {
			ExceptionUtils.processSoapException(resp, e);
			return resp;
		}
		Integer resultCode = sdkResponse.value;
		resp.setResult(0 == resultCode ? sitesInfo.value : null);
		resp.setResultCode(resultCode);
		return resp;
	}

	
	public TPSDKResponse<SiteInfo> querySiteByURI(String siteUri) {
		Holder<Integer> sdkResponse = new Holder<Integer>();
		TPSDKResponse<SiteInfo> resp = new TPSDKResponse<SiteInfo>();
		Holder<SiteInfo> sitesInfo = new Holder<SiteInfo>();
		try {
			tPHealthcareSiteMgr.querySiteByURI(siteUri, sdkResponse, sitesInfo);
		} catch (Exception e) {
			ExceptionUtils.processSoapException(resp, e);
			return resp;
		}
		Integer resultCode = sdkResponse.value;
		resp.setResult(0 == resultCode ? sitesInfo.value : null);
		resp.setResultCode(resultCode);
		return resp;
	}

	
	public TPSDKResponse<List<SiteInfo>> querySiteByName(String siteName) {
		Holder<Integer> sdkResponse = new Holder<Integer>();
		TPSDKResponse<List<SiteInfo>> resp = new TPSDKResponse<List<SiteInfo>>();
		Holder<List<SiteInfo>> sitesInfo = new Holder<List<SiteInfo>>();
		try {
			tPHealthcareSiteMgr.querySiteByName(siteName, sdkResponse,
					sitesInfo);
		} catch (Exception e) {
			ExceptionUtils.processSoapException(resp, e);
			return resp;
		}
		Integer resultCode = sdkResponse.value;
		resp.setResult(0 == resultCode ? sitesInfo.value : null);
		resp.setResultCode(resultCode);
		return resp;
	}

	
	public TPSDKResponse<Integer> querySiteFreeBusy(String siteUri) {
		Holder<Integer> sdkResponse = new Holder<Integer>();
		TPSDKResponse<Integer> resp = new TPSDKResponse<Integer>();
		Holder<Integer> state = new Holder<Integer>();
		if (null == siteUri) {
			resp.setResultCode(NativeConstant.SITEURI_IS_NULL);
			return resp;
		}
		try {
			tPHealthcareSiteMgr.querySiteFreeBusy(siteUri, sdkResponse, state);
		} catch (Exception e) {
			ExceptionUtils.processSoapException(resp, e);
			return resp;
		}
		Integer resultCode = sdkResponse.value;
		resp.setResult(0 == resultCode ? state.value : null);
		resp.setResultCode(resultCode);
		return resp;
	}

	
	public TPSDKResponse<Map<String, Integer>> querySitesFreeBusy(
			List<String> siteUris, Integer timeRange) {
		Holder<Integer> sdkResponse = new Holder<Integer>();
		TPSDKResponse<Map<String, Integer>> resp = new TPSDKResponse<Map<String, Integer>>();
		Holder<List<SitesStatusMap>> sitesFreeBusyState = new Holder<List<SitesStatusMap>>();
		try {
			tPHealthcareSiteMgr.querySitesFreeBusy(siteUris, timeRange,
					sdkResponse, sitesFreeBusyState);
		} catch (Exception e) {
			ExceptionUtils.processSoapException(resp, e);
			return resp;
		}
		Integer resultCode = sdkResponse.value;
		if (0 == resultCode) {
			Map<String, Integer> map = new HashMap<String, Integer>();
			for (SitesStatusMap status : sitesFreeBusyState.value) {
				map.put(status.getSiteUri(), status.getSiteStatus());
			}
			resp.setResult(map);
		}
		resp.setResultCode(resultCode);
		return resp;
	}

	
	public Integer ctrlCamera(String siteUri, CameraControl cameraControl) {
		try {
			return tPHealthcareSiteCtr.ctrlCamera(siteUri, cameraControl);
		} catch (Exception e) {
			return ExceptionUtils.processSoapException(e);
		}
	}

	
	public Integer ctrlAuxStream(String siteUri, Integer controlCode) {
		try {
			return tPHealthcareSiteCtr.ctrlAuxStream(siteUri, controlCode);
		} catch (Exception e) {
			return ExceptionUtils.processSoapException(e);
		}
	}

	
	public Integer micMuteSites(String confId, List<String> siteUris,
			Boolean isMute) {
		try {
			return tPHealthcareSiteCtr.micMuteSites(confId, siteUris, isMute);
		} catch (Exception e) {
			return ExceptionUtils.processSoapException(e);
		}
	}

	
	public Integer spkQuietSites(String confId, List<String> siteUris,
			Boolean isQuiet) {
		try {
			return tPHealthcareSiteCtr.spkQuietSites(confId, siteUris, isQuiet);
		} catch (Exception e) {
			return ExceptionUtils.processSoapException(e);
		}
	}

	
	public Integer connectSites(String confId, List<String> siteUris) {
		try {
			return tPHealthcareSiteCtr.connectSites(confId, siteUris);
		} catch (Exception e) {
			return ExceptionUtils.processSoapException(e);
		}
	}

	
	public Integer disconnectSites(String confId, List<String> siteUris) {
		try {
			return tPHealthcareSiteCtr.disconnectSites(confId, siteUris);
		} catch (Exception e) {
			return ExceptionUtils.processSoapException(e);
		}
	}

}
